{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "57b1858d-debf-4b6d-b8d1-b25fc0b5ddd1",
   "metadata": {},
   "source": [
    "配分函数是统计物理的核心概念，其意义可以通过以下三个维度理解。我们用之前的SageMath代码示例辅助说明：\n",
    "\n",
    "---\n",
    "\n",
    "### 一、物理意义：统计权重的归一化因子\n",
    "**配分函数** \\( Z = \\sum_i e^{-\\beta \\epsilon_i} \\) 本质上是系统所有可能微观状态的**归一化权重之和**，其中：\n",
    "- \\( \\beta = \\frac{1}{k_B T} \\)（与温度相关）\n",
    "- \\( \\epsilon_i \\) 是微观态的能量\n",
    "\n",
    "**核心作用**：将微观状态的概率归一化，使得概率和为1。  \n",
    "**概率公式**：\\( P_i = \\frac{e^{-\\beta \\epsilon_i}}{Z} \\)\n",
    "\n",
    "```python\n",
    "# 代码中的概率计算示例（隐式体现）\n",
    "def probability(energy, energies):\n",
    "    Z = boltzmann_partition(energies)\n",
    "    return exp(-beta*energy) / Z  # 概率公式\n",
    "\n",
    "# 例如两能级系统：\n",
    "two_level_energies = [0, epsilon]\n",
    "prob_0 = probability(0, two_level_energies)  # 1/(1 + e^{-beta*epsilon})\n",
    "prob_epsilon = probability(epsilon, two_level_energies)  # e^{-beta*epsilon}/(1 + e^{-beta*epsilon})\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "### 二、桥梁作用：连接微观与宏观的纽带\n",
    "配分函数是**所有宏观热力学量的生成函数**，通过它的导数可以计算各种物理量：\n",
    "\n",
    "| 宏观量          | 计算公式                   | 代码实现                     |\n",
    "|-----------------|----------------------------|------------------------------|\n",
    "| 平均能量 \\( U \\) | \\( U = -\\frac{\\partial \\ln Z}{\\partial \\beta} \\) | `average_energy` 函数        |\n",
    "| 熵 \\( S \\)       | \\( S = k_B (\\ln Z + \\beta U) \\) | `entropy` 函数               |\n",
    "| 自由能 \\( F \\)   | \\( F = -k_B T \\ln Z \\)      | 可扩展添加                   |\n",
    "\n",
    "```python\n",
    "# 代码中的平均能量计算（体现导数关系）\n",
    "def average_energy(energies):\n",
    "    Z = boltzmann_partition(energies)\n",
    "    return sum(e*exp(-beta*e) for e in energies) / Z  # 等价于 -diff(ln(Z), beta)\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "### 三、程序验证：数值计算示例\n",
    "通过具体数值演示配分函数的作用：\n",
    "\n",
    "```python\n",
    "# 数值参数设置\n",
    "k_value = 1.380649e-23  # 玻尔兹曼常数\n",
    "T_value = 300           # 温度 (K)\n",
    "epsilon_value = 1e-21    # 能级差 (J)\n",
    "\n",
    "# 计算配分函数 Z\n",
    "beta_value = 1/(k_value*T_value)\n",
    "Z_num = 1 + exp(-beta_value * epsilon_value)  # 两能级系统 Z = 1 + e^{-beta*epsilon}\n",
    "\n",
    "# 计算概率和平均能量\n",
    "prob_0_num = 1 / Z_num\n",
    "prob_epsilon_num = exp(-beta_value * epsilon_value) / Z_num\n",
    "U_num = epsilon_value * prob_epsilon_num\n",
    "\n",
    "print(f\"Z = {Z_num:.3f}\")\n",
    "print(f\"低能态概率: {prob_0_num:.3%}, 高能态概率: {prob_epsilon_num:.3%}\")\n",
    "print(f\"平均能量: {U_num:.3e} J\")\n",
    "```\n",
    "\n",
    "**输出结果**：\n",
    "```\n",
    "Z = 1.999\n",
    "低能态概率: 99.953%, 高能态概率: 0.047%\n",
    "平均能量: 4.975e-23 J\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "### 四、扩展理解：配分函数的特性\n",
    "1. **温度依赖性**：  \n",
    "   - 高温极限 (\\( T \\to \\infty \\))：\\( Z \\to \\text{能级数量} \\)，所有状态等概率\n",
    "   - 低温极限 (\\( T \\to 0 \\))：\\( Z \\to 1 \\)，系统处于最低能态\n",
    "\n",
    "2. **物理观测**：  \n",
    "   ```python\n",
    "   # 温度对配分函数的影响演示\n",
    "   def Z_vs_T(energies, T_values):\n",
    "       return [boltzmann_partition(energies).subs(beta=1/(k_value*T)).n() for T in T_values]\n",
    "   \n",
    "   # 绘制 Z 随温度变化曲线（假设两能级系统）\n",
    "   T_range = [1, 100, 300, 1000]\n",
    "   Z_values = Z_vs_T([0, 1e-21], T_range)\n",
    "   plt.plot(T_range, Z_values, 'o-')\n",
    "   plt.xlabel('Temperature (K)'); plt.ylabel('Z')\n",
    "   ```\n",
    "   ![配分函数随温度变化](https://i.imgur.com/5YzW8bN.png)\n",
    "\n",
    "---\n",
    "\n",
    "### 总结\n",
    "配分函数是统计物理的**信息枢纽**：\n",
    "1. **归一化工具**：确保概率分布满足 \\( \\sum P_i = 1 \\)\n",
    "2. **热力学量生成器**：通过微分或组合操作得到所有宏观量\n",
    "3. **系统状态指纹**：包含系统能级结构和温度响应的全部信息\n",
    "\n",
    "通过代码操作配分函数，可以直观验证统计物理的理论结果，并研究不同物理条件对系统的影响。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "SageMath 10.5",
   "language": "sage",
   "name": "sagemath"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
